#include "NodeContainerItemManager.h"

using namespace cocos2d;
using namespace ui;

CNodeContainerItemManager::CNodeContainerItemManager(void) : m_Container(nullptr)
{
}


CNodeContainerItemManager::~CNodeContainerItemManager(void)
{
	for (size_t i = 0; i != m_Items.size(); ++i) {
		delete m_Items[i];
	}
	m_Items.clear();
}

void CNodeContainerItemManager::PushItem(cocos2d::Node* node) {
	ContainerItem* tItem = new ContainerItem;
	node->setUserData(tItem);
	tItem->m_node = node;
	m_Items.push_back(tItem);
}

void CNodeContainerItemManager::UpdatePage(uint8_t box, uint8_t count) {
	if (nullptr == m_Container) {
		assert(false);
		return;
	}

	auto tCheckGoodsFunc = [&](ContainerItem* item_, const GoodsInfo& now_)->void {
		m_TempItemContent.Clean();
		if (item_->m_goods.Equal(now_)) {
			if (item_->m_goods.m_Number != now_.m_Number)
				m_TempItemContent.SetCount(now_.m_Number);
		} else {
			m_TempItemContent.SetSide(cocos2d::Color3B::BLUE);
			m_TempItemContent.SetCount(now_.m_Number);
			m_TempItemContent.SetBack("");
		}

		if (m_TempItemContent.m_ChangeFlag != 0){
			item_->m_goods = now_;
			m_UpdateItem(item_->m_node, m_TempItemContent);
		}
	};

	uint8_t tTotalSize = 0;
	for (uint8_t i = 0; i != count; ++i) {
		for (uint8_t j = 0; j != NodeCOUNT; ++j) {
			do 
			{
				const CGLGoodsBase* tGoods = m_Container->GetGoods(box, j);
				ContainerItem* tItem = m_Items[tTotalSize];
				if (nullptr == tGoods)
					tCheckGoodsFunc(tItem, m_TempInvalid);
				else {
					tCheckGoodsFunc(tItem, tGoods->GetGoodsInfo());
					tItem->m_ContainerBox = box;
					tItem->m_ContainerIndex = j;
				}
			} while (0);
			++tTotalSize;
		}
		++box;
	}
}

void CNodeContainerItemManager::onShowItemDetail(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {

}
